//
//  BaseViewController.m
//  KuaiTu
//
//  Created by 江伟 on 2018/9/17.
//  Copyright © 2018年 WeiJiang. All rights reserved.
//

#import "BaseViewController.h"
@interface BaseViewController ()
@property (nonatomic,copy) NSString *myEmptyTitle;
@property (nonatomic,strong) UIImage *myEmptyImg;
@end

@implementation BaseViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _emptyImg = IMG(@"empty");
        _myEmptyImg = IMG(@"empty");
        _emptyTitle = @"暂无数据,点击刷新";
        _myEmptyTitle = @"暂无数据,点击刷新";
        _isLoading = YES;
        [self addNotification];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NetWorkLoadingNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NetWorkLoadingEndNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!_isHiddenNav) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 登录失效时会自动跳转登录页面这个时候应该结束上拉刷新等
    [self endRefresh];
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = viewColor;
    [self setupViews];
    [self loadData];
    //取消登陆
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(CancelLanding) name:CancelLandingNotification object:nil];
    // Do any additional setup after loading the view.
}


- (void)setupViews {}

- (void)loadData {}

- (void)setupRefresh {}

- (void)endRefresh {}

// MARK: -- 网络加载通知

- (void)addNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkLoading:) name:NetWorkLoadingNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkLoadingEnd:) name:NetWorkLoadingEndNotification object:nil];
}

/// 网络开始加载
- (void)netWorkLoading:(NSNotification *)notification {
    self.isLoading = YES;
}

/// 网络加载完成
- (void)netWorkLoadingEnd:(NSNotification *)notification {
    NSDictionary *dic = notification.object;
    if ([dic[@"isSuccess"] integerValue] == 0) {  //网络失败
        self.myEmptyTitle = NET_FAIL;
        self.myEmptyImg = IMG(@"wangluoshibai");
    } else {
        self.myEmptyTitle = self.emptyTitle;
        self.myEmptyImg = self.emptyImg;
    }
    self.isLoading = NO;
    /// 显示占位图
    /*
     占位图一般都只在刚进入页面起作用
     */
    if (_tableView) { [self.tableView reloadEmptyDataSet]; }
    if (_collectionView) { [self.collectionView reloadEmptyDataSet]; }
}

// MARK: -- 实现tableView section代理 去掉多出的空白
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

// MARK: -- EmptyDataSet占位图

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return self.isLoading ? IMG(@"") : self.myEmptyImg;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = self.isLoading ? @"" : self.myEmptyTitle;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:R_Font(14),
                                 NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#b3b3b3"]
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self loadData];
    [scrollView reloadEmptyDataSet];
    NSLog(@"点击试图刷新");
}

// MARK: -- 设置导航栏按钮

/**
 设置导航栏左边按钮的文字
 */
- (void)setNavLeftBtnWithTitle:(NSString*)title {
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:title forState:UIControlStateNormal];
    leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    leftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [leftBtn setFrame:CGRectMake(0, 0, 80, 40)];
    [leftBtn addTarget:self action:@selector(onTapNavLeftBtn) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitle:title forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    UIBarButtonItem *leftBarStarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    self.navigationItem.leftBarButtonItem = leftBarStarButtonItem;
}

/**
 设置导航栏左边按钮的图标
 */
- (void)setNavLeftBtnWithImageName:(NSString*)imageName {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:IMG(imageName) style:UIBarButtonItemStyleDone target:self action:@selector(onTapNavLeftBtn)];
}

- (void)onTapNavLeftBtn {
//    [self back];
}

/**
 设置导航栏右边按钮的文字
 */
- (void)setNavRightBtnWithTitle:(NSString*)title {
    //    self.navigationItem.rightBarButtonItem = [backBarItem shareRight:self:title];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:title forState:UIControlStateNormal];
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [rightBtn setFrame:CGRectMake(10, 0, 80, 40)];
    [rightBtn addTarget:self action:@selector(onTapNavRightBtn) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:title forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarStarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarStarButtonItem;
}

/**
 设置导航栏右边按钮的图标
 */
- (void)setNavRightBtnWithImageName:(NSString*)imageName
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 60, 40);
    [btn setImage:IMG(imageName) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onTapNavRightBtn) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarStarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.navigationItem.rightBarButtonItem = rightBarStarButtonItem;
}

- (void)setNavRightTwoBtnsWithImageName1:(NSString*)imageName1 andName2:(NSString*)imageName2 {
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 0, 22, 22);
    [btn1 addTarget:self action:@selector(imageName1Click) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarStarButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:btn1];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(0, 0, 22, 22);
    [btn2 addTarget:self action:@selector(imageName2Click) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarStarButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:btn2];
    
    UIImageView *starBtnIV = [[UIImageView alloc] init];
    starBtnIV.frame = CGRectMake(7,3,24,24);
    starBtnIV.image = [UIImage imageNamed:imageName1];
    [btn1 addSubview:starBtnIV];
    
    
    UIImageView *starBtnIV2 = [[UIImageView alloc] init];
    starBtnIV2.frame = CGRectMake(7,3, 24, 24);
    starBtnIV2.image = [UIImage imageNamed:imageName2];
    [btn2 addSubview:starBtnIV2];
    
    self.navigationItem.rightBarButtonItems = @[rightBarStarButtonItem1,rightBarStarButtonItem2];
}

- (void)onTapNavRightBtn {
    [self.view endEditing:YES];
}

- (void)imageName1Click {
    
}

- (void)imageName2Click {
    
}
#pragma mark actions （点击事件）
- (void)CancelLanding {
    
    
    self.tabBarController.selectedIndex = 0;
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)backToRootVC {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

// MARK: -- 懒加载

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, ViewHeight1) style: UITableViewStyleGrouped];
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.separatorColor = lineColor1;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (UIScrollView *)scroView {
    if (!_scroView) {
        _scroView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KWidth, ViewHeight1)];
        _scroView.delegate = self;
        _scroView.pagingEnabled = YES;
        _scroView.showsVerticalScrollIndicator = NO;
        _scroView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_scroView];
    }
    return _scroView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
